import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import firstLeval from '../views/firstLeval/route'
import homeSecond from '../views/homeSecond/route'
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      ...firstLeval
    ]
  },
  ...homeSecond
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 路由全局前置守卫
router.beforeEach((to, from) => {
  // console.log(to, from);
  let user = localStorage.getItem('user')
  // to.matched.some(item => {
  //   console.log(item.meta.needLogin);
  // })
  if(to.matched.some(item => item.meta.needLogin)) {
    // console.log('用户需要登录才行');
    if(user) { // 判断是否有用户信息 如果有证明用户已经登录
      return true
    } else { // 否则表示用户还未登录 直接进入登录页面
      // console.log('用户需要登录才行');
      return {
        path: '/profile'
      }
    }
  }
  return true
})

router.afterEach((to, from) => {
  document.querySelector('title').innerText = to.meta.title
})
export default router
